import { Article } from '@libs/db/models/article.model';
import { Controller, Get, Param } from '@nestjs/common';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { ModelType } from '@typegoose/typegoose/lib/types';
import { InjectModel } from 'nestjs-typegoose';

@ApiTags('文章接口')
@Controller('articles')
export class ArticlesController {
  constructor(
    @InjectModel(Article) private readonly ArticleMode: ModelType<Article> 
  ) {}

  @Get('newList')
  @ApiOperation({summary: '获取最新的文章列表'})
  async newList() {
    // const data = await this.ArticleMode.aggregate([{$sample: {size: 5}}]);
    const data = await this.ArticleMode.find().sort({_id: -1}).limit(5);
    return data;
  }

  @Get('AllList')
  @ApiOperation({summary: '获取所有文章列表'})
  async AllList() {
    const data = await this.ArticleMode.find();
    return data;
  }

  @Get('dailyList')
  @ApiOperation({summary: '获取日常的文章列表'})
  async dailyList() {
    const data = await this.ArticleMode.find({'category': '日常'}).sort({_id: -1}).limit(5);
    return data;
  }

  @Get('activityList')
  @ApiOperation({summary: '获取活动的文章列表'})
  async activityList() {
    // const data = await this.ArticleMode.aggregate([{$sample: {size: 5}}]);
    const data = await this.ArticleMode.find({'category': '活动'}).sort({_id: -1}).limit(5);
    return data;
  }

  @Get('noticeList')
  @ApiOperation({summary: '获取公告的文章列表'})
  async noticeList() {
    // const data = await this.ArticleMode.aggregate([{$sample: {size: 5}}]);
    const data = await this.ArticleMode.find({'category': '公告'}).sort({_id: -1}).limit(5);
    return data;
  }

  @Get('matchList')
  @ApiOperation({summary: '获取赛事的文章列表'})
  async matchList() {
    // const data = await this.ArticleMode.aggregate([{$sample: {size: 5}}]);
    const data = await this.ArticleMode.find({'category': '赛事'}).sort({_id: -1}).limit(5);
    return data;
  }

  @Get(':id')
  @ApiOperation({summary: '根据id获取文章'})
  async getId(@Param('id') id: string) {
    const data = await this.ArticleMode.find({_id: id});
    return data;
  }
}
